package com.hongye.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验器
 * 可以校验 StatusValid 注解
 * 校验类型为Integer
 * @author lins
 * @date 2021/10/15 15:26
 */
public class StatusValidConstraintValidator implements ConstraintValidator<StatusValid, Integer> {
    Set<Integer> set = new HashSet<Integer>();
    @Override
    public void initialize(StatusValid constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            set.add(value);
        }
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
